<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>异常详情 - 智能水表抄表系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/static/styles/main.css">
</head>

<body>
    <header class="header">
        <h1><i class="fas fa-tint"></i> 智能水表抄表系统</h1>
    </header>

    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm">
        <div class="container">
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/"><i class="fas fa-home"></i> 首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/history"><i class="fas fa-history"></i> 历史记录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/bills"><i class="fas fa-file-invoice-dollar"></i> 账单管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/anomalies"><i class="fas fa-exclamation-triangle"></i> 异常记录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/water_plans"><i class="fas fa-project-diagram"></i> 水务规划</a>
                    </li>
                    {% if user and user.role == "admin" %}
                    <li class="nav-item">
                        <a class="nav-link" href="/admin"><i class="fas fa-user-shield"></i> 管理控制台</a>
                    </li>
                    {% endif %}
                </ul>
                <div class="navbar-nav">
                    <span class="nav-item nav-link">
                        <i class="fas fa-user"></i> {{ user.username }}
                    </span>
                    <a class="nav-link" href="/logout"><i class="fas fa-sign-out-alt"></i> 退出登录</a>
                </div>
            </div>
        </div>
    </nav>

    <div class="container py-4">
        <div class="row mb-4">
            <div class="col-md-12">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="/anomalies">异常记录</a></li>
                        <li class="breadcrumb-item active" aria-current="page">异常详情</li>
                    </ol>
                </nav>
                <div class="d-flex justify-content-between align-items-center">
                    <h2><i class="fas fa-exclamation-triangle"></i> 异常详情</h2>
                    <div>
                        <a href="/anomalies" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> 返回列表
                        </a>
                        {% if anomaly.status == 'unresolved' and user.role == 'admin' %}
                        <button type="button" class="btn btn-success" data-bs-toggle="modal"
                            data-bs-target="#resolveModal">
                            <i class="fas fa-check"></i> 解决异常
                        </button>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        {% if error %}
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ error }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endif %}

        <div class="row">
            <div class="col-md-8">
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-light">
                        <h3 class="card-title mb-0">
                            <i class="fas fa-info-circle"></i> 异常信息
                            {% if anomaly.status == 'resolved' %}
                            <span class="badge bg-success float-end">已解决</span>
                            {% else %}
                            <span class="badge bg-warning float-end">待处理</span>
                            {% endif %}
                        </h3>
                    </div>
                    <div class="card-body">
                        <div class="alert alert-{{ 'success' if anomaly.status == 'resolved' else 'warning' }}">
                            {% if anomaly.anomaly_type == 'reading_decreased' %}
                            <i class="fas fa-arrow-down"></i> <strong>读数减少异常</strong>: 当前读数小于上次读数
                            {% elif anomaly.anomaly_type == 'excessive_usage' %}
                            <i class="fas fa-tint"></i> <strong>用水量异常</strong>: 用水量超出正常范围
                            {% elif anomaly.anomaly_type == 'invalid_reading' %}
                            <i class="fas fa-exclamation-circle"></i> <strong>无效读数</strong>: 读数无法解析
                            {% else %}
                            <i class="fas fa-question-circle"></i> <strong>其他异常</strong>: {{ anomaly.anomaly_type }}
                            {% endif %}
                        </div>

                        <table class="custom-table">
                            <tbody>
                                <tr>
                                    <th width="30%">异常ID</th>
                                    <td>{{ anomaly.id }}</td>
                                </tr>
                                <tr>
                                    <th>水表编码</th>
                                    <td>{{ anomaly.meter_code }}</td>
                                </tr>
                                <tr>
                                    <th>关联读数ID</th>
                                    <td>{{ anomaly.reading_id }}</td>
                                </tr>
                                <tr>
                                    <th>异常描述</th>
                                    <td>{{ anomaly.description }}</td>
                                </tr>
                                <tr>
                                    <th>状态</th>
                                    <td>
                                        {% if anomaly.status == 'resolved' %}
                                        <span class="badge bg-success">已解决</span>
                                        {% else %}
                                        <span class="badge bg-warning">待处理</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <th>报告日期</th>
                                    <td>{{ anomaly.report_date }}</td>
                                </tr>
                                {% if anomaly.status == 'resolved' %}
                                <tr>
                                    <th>解决日期</th>
                                    <td>{{ anomaly.resolved_date }}</td>
                                </tr>
                                {% endif %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-light">
                        <h3 class="card-title mb-0"><i class="fas fa-lightbulb"></i> 处理建议</h3>
                    </div>
                    <div class="card-body">
                        {% if anomaly.status == 'resolved' %}
                        <div class="alert alert-success">
                            <i class="fas fa-check-circle"></i> 此异常已解决
                        </div>
                        {% else %}
                        <div class="alert alert-warning">
                            <i class="fas fa-exclamation-triangle"></i> 此异常需要处理
                        </div>
                        {% endif %}

                        <div class="mb-4">
                            <h5>可能原因：</h5>
                            <ul class="list-group">
                                {% if anomaly.anomaly_type == 'reading_decreased' %}
                                <li class="list-group-item">
                                    <i class="fas fa-tools"></i> 水表可能被人为调整
                                </li>
                                <li class="list-group-item">
                                    <i class="fas fa-camera"></i> 图像识别错误
                                </li>
                                <li class="list-group-item">
                                    <i class="fas fa-cogs"></i> 水表故障
                                </li>
                                {% elif anomaly.anomaly_type == 'excessive_usage' %}
                                <li class="list-group-item">
                                    <i class="fas fa-tint"></i> 用户家中可能有水管漏水
                                </li>
                                <li class="list-group-item">
                                    <i class="fas fa-users"></i> 用户可能在短时间内使用大量水
                                </li>
                                <li class="list-group-item">
                                    <i class="fas fa-camera"></i> 图像识别错误
                                </li>
                                {% elif anomaly.anomaly_type == 'invalid_reading' %}
                                <li class="list-group-item">
                                    <i class="fas fa-camera"></i> 图像质量太差
                                </li>
                                <li class="list-group-item">
                                    <i class="fas fa-eye-slash"></i> 水表显示不清晰
                                </li>
                                <li class="list-group-item">
                                    <i class="fas fa-cogs"></i> 水表故障
                                </li>
                                {% endif %}
                            </ul>
                        </div>

                        {% if anomaly.status == 'resolved' %}
                        <div>
                            <h5>解决方案：</h5>
                            <p>{{ anomaly.description|replace('|', '<br>')|safe }}</p>
                        </div>
                        {% else %}
                            {% if user.role == 'admin' %}
                            <div class="d-grid gap-2">
                                <button class="btn btn-primary" type="button" data-bs-toggle="modal"
                                    data-bs-target="#resolveModal">
                                    <i class="fas fa-check-circle"></i> 解决此异常
                                </button>
                            </div>
                            {% else %}
                            <div class="alert alert-info">
                                <i class="fas fa-info-circle"></i> 此异常需要管理员处理
                            </div>
                            {% endif %}
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 解决异常的模态框 -->
    {% if anomaly.status == 'unresolved' and user.role == 'admin' %}
    <div class="modal fade" id="resolveModal" tabindex="-1" aria-labelledby="resolveModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="/resolve_anomaly/{{ anomaly.id }}" method="POST">
                    <div class="modal-header">
                        <h5 class="modal-title" id="resolveModalLabel">解决异常 #{{ anomaly.id }}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="resolution" class="form-label">解决方案</label>
                            <textarea class="form-control" id="resolution" name="resolution" rows="4" required></textarea>
                            <div class="form-text">请详细描述如何解决此异常</div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-success">确认解决</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    {% endif %}

    <footer class="footer">
        <div class="container">
            <p>&copy; 2023 智能水表抄表系统 | 提供水表读数和编码识别</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
</body>

</html> 